<template>
  <el-dialog title="采购计划选择"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal= true
    width="80%"
    center
  >
    <el-row :gutter="20">
      <!--采购计划数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="计划编码" prop="planNumber">
            <el-input
              v-model="queryParams.planNumber"
              placeholder="采购计划编码"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="计划名称" prop="planName">
            <el-input
              v-model="queryParams.planName"
              placeholder="请输入采购计划名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="计划日期" prop="planDate">
            <el-date-picker clearable
                            v-model="queryParams.planDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择计划日期"
                            @keyup.enter.native="handleQuery">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="截止日期" prop="materialDeadline">
            <el-date-picker clearable
                            v-model="queryParams.materialDeadline"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择物料需求截止日期"
                            @keyup.enter.native="handleQuery">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table border v-loading="loading" :data="planList" @current-change="handleCurrent" @row-dblclick="handleRowDbClick">
          <el-table-column width="50" align="center" >
              <template v-slot="scope">
                  <el-radio v-model="selectedPlanId" :label="scope.row.planId" @change="handleRowChange(scope.row)">{{""}}</el-radio>
              </template>
          </el-table-column>
          <el-table-column label="采购计划编号"  align="center" key="planNumber" prop="planNumber" v-if="columns[0].visible" >
          </el-table-column>
          <el-table-column label="采购计划名称"  align="left" key="planName" prop="planName" v-if="columns[1].visible" :show-overflow-tooltip="true" />
          <el-table-column label="采购计划日期" align="left" key="planDate" prop="planDate" v-if="columns[2].visible" :show-overflow-tooltip="true" />
          <el-table-column label="需求截止日期" align="center" key="materialDeadline" prop="materialDeadline" v-if="columns[3].visible" :show-overflow-tooltip="true" />
<!--          <el-table-column label="物料/产品" align="center" key="itemOrProduct" prop="itemOrProduct" v-if="columns[4].visible" :show-overflow-tooltip="true" >
            <template slot-scope="scope">
              <dict-tag :options="dict.type.mes_item_product" :value="scope.row.itemOrProduct"/>
            </template>
          </el-table-column>
          <el-table-column label="所属分类" align="center" key="itemTypeName" prop="itemTypeName" v-if="columns[5].visible" width="120" />
          <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[6].visible" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>-->
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmSelect">确 定</el-button>
        <el-button @click="showFlag=false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listPlan} from "@/api/mes/cg/plan";

export default {
  name: "PlanSelect",
  data() {
    return {
      showFlag:false,
      // 选中数组
      selectedPlanId: undefined,
      selectedRows: undefined,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料产品表格数据
      planList: null,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planNumber: undefined,
        planName: undefined,
        planDate: undefined,
        materialDeadline: undefined,
        approvalStatus:1
      },
      // 列信息
      columns: [
        { key: 0, label: `采购计划编号`, visible: true },
        { key: 1, label: `采购计划名称`, visible: true },
        { key: 2, label: `采购计划日期`, visible: true },
        { key: 3, label: `需求截止日期`, visible: true },
      ]
    };
  },
  watch: {
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物料编码列表 */
    getList() {
      this.loading = true;
      listPlan(this.queryParams).then(response => {
          this.planList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleCurrent(row){
        if(row){
            this.selectedRows = row;
        }
    },
    handleRowDbClick(row){
      if(row){
        this.selectedRows = row;
        this.$emit('onSelected',this.selectedRows);
        this.showFlag = false;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if(row){
        this.selectedRows = row;
      }
    },
    //确定选中
    confirmSelect(){
        if(this.selectedPlanId ==null || this.selectedPlanId==0){
            this.$notify({
                title:'提示',
                type:'warning',
                message: '请至少选择一条数据!'
            });
            return;
        }
        this.$emit('onSelected',this.selectedRows);
        this.showFlag = false;
    }
  }
};
</script>
